package com.management.controller;

import com.management.po.Commodity;
import com.management.po.CommodityType;
import com.management.po.Company;
import com.management.service.CommodityService;
import com.management.service.CommodityTypeService;
import com.management.service.CompanyService;
import com.management.vo.CommodityTypeVO;
import com.management.vo.CommodityVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version v1.0
 * @Author: Slark
 * @Date: Created in2021/11/1  10:13
 * 商品表页面控制器
 */
@Controller
@RequestMapping("/goods")
public class CommodityController {

    Logger logger=Logger.getLogger(CommodityController.class);

    @Autowired
    CommodityService commodityService;
    @Autowired
    CommodityTypeService commodityTypeService;
    @Autowired
    CompanyService companyService;


    /*展示所有商品*/
    @RequestMapping("/query/tocommoditylist")
    public String tocommoditylist(Model model){
        logger.info("进入tocommoditylist");
        List<CommodityVO> list= commodityService.findAll();
        model.addAttribute("list",list);
        return "commoditylist";
    }
    /*展示该公司旗下的所有商品*/
    @RequestMapping("/query/tocompanyidlist")
    public String tocompanyidlist(int companyId,Model model){
        logger.info("进入公司商品列表控制器");
        List<CommodityVO> list= commodityService.findByCompanyId(companyId);
        model.addAttribute("list",list);
        return "commoditylist";
    }


    /*添加商品*/
    @RequestMapping("/increase/toaddcommodity")
    public String toaddcommodity(int companyId,Model model){
        logger.info("进入去添加商品控制器");
        List<CommodityTypeVO> list= commodityTypeService.findByCompanyId(companyId);
        Company company=companyService.findByCompanyId(companyId);
        logger.info("list:"+list);
        logger.info(company);
        model.addAttribute("list",list);
        model.addAttribute("company",company);
        return "addcommodity";
    }
    /*添加商品*/
    @RequestMapping("/increase/addcommodity")
    public String addcommodity(Commodity commodity, int companyId){
        logger.info("进入添加商品控制器");
        boolean flag= commodityService.add(commodity);
        logger.info("添加"+flag);

        return "redirect:http://localhost:8080/management/goods/query/tocompanyidlist.action?companyId="+companyId;
    }
    /*去修改商品*/
    @RequestMapping("/editor/toupdatecommodity")
    public String toupdatecommodity(int commodityId,Model model){
        logger.info("进入去修改控制器");
        CommodityVO commodityVO=commodityService.findById(commodityId);
        logger.info(commodityVO);
        model.addAttribute("commodityVO",commodityVO);
        List<CommodityTypeVO> list= commodityTypeService.findAll();
        logger.info(list);
        model.addAttribute("list",list);
        return "updatecommodity";
    }
    /*去修改商品*/
    @RequestMapping("/editor/updatecommodity")
    public String updatecommodity(Commodity commodity,int companyId){
        logger.info("进入修改控制器");
        boolean flag=commodityService.update(commodity);
        logger.info(commodity.getCommodityId());
        System.out.println(commodity.getCommodityId());
        logger.info("更新"+flag);

        return "pages";
    }
}
